package org.vaadinpa.logic.commands;

import org.vaadinpa.Conf;
import org.vaadinpa.data.NewsItem;
import org.vaadinpa.data.Planet;
import org.vaadinpa.data.Resource;
import org.vaadinpa.exceptions.NotEnoughFundsException;
import org.vaadinpa.logic.Command;

public class AsteroidScanCreateCommand extends Command implements Cloneable {

	public final int num;

	public AsteroidScanCreateCommand(Planet from, int num) throws NotEnoughFundsException {
		super(from);

		this.num = num;

		// withdraw resources
		int metal = Conf.COST_ROID_SCAN[Resource.METAL.ordinal()];
		int crystal = Conf.COST_ROID_SCAN[Resource.CRYSTAL.ordinal()];
		int gold = Conf.COST_ROID_SCAN[Resource.GOLD.ordinal()];
		planet.withdrawFunds(metal, crystal, gold);
	}

	@Override
	public void execute() {

		// add scans
		planet.availableRoidScans += num;

		NewsItem i = new NewsItem("Asteroid Scans Available", "You now have " + num + " new Asteroid Scans to use.");
		planet.addNews(i);
	}

	@Override
	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void cancel() {
		// return funds
		int metal = Conf.COST_ROID_SCAN[Resource.METAL.ordinal()];
		int crystal = Conf.COST_ROID_SCAN[Resource.CRYSTAL.ordinal()];
		int gold = Conf.COST_ROID_SCAN[Resource.GOLD.ordinal()];
		planet.depositFunds(metal, crystal, gold);
	}

	@Override
	public String getDescription() {
		return "Building " + num + " scans";
	}

	@Override
	public String getName() {
		return "Roid scan order";
	}

	@Override
	public void serialize(StringBuilder sb) {
		super.serializeBase(sb);

		sb.append("<num>" + num + "</num>");
		sb.append("</" + this.getClass().getSimpleName() + ">");
	}
}
